package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.TaskRecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 任务完成详情适配器
 */
public class TaskCompletionAdapter extends RecyclerView.Adapter<TaskCompletionAdapter.ViewHolder> {

    private Context context;
    private List<TaskRecord> taskRecords;
    private SimpleDateFormat inputFormat;
    private SimpleDateFormat outputFormat;

    public TaskCompletionAdapter(Context context, List<TaskRecord> taskRecords) {
        this.context = context;
        this.taskRecords = taskRecords;
        this.inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        this.outputFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_completion, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskRecord record = taskRecords.get(position);
        
        // 设置任务名称
        holder.tvTaskName.setText(record.getTaskName());
        
        // 设置任务描述
        if ("用户上传任务".equals(record.getRemark())) {
            // 这是用户发布的任务被其他用户完成
            holder.tvTaskDescription.setText(record.getTaskDescription() + " [我发布的]");
        } else {
            // 这是用户自己完成的任务
            holder.tvTaskDescription.setText(record.getTaskDescription());
        }
        
        // 设置任务奖励
        holder.tvReward.setText(String.format("+¥%.2f", record.getReward()));
        
        // 设置任务状态
        holder.tvStatus.setText(record.getStatusText());
        switch (record.getStatus()) {
            case TaskRecord.STATUS_APPROVED:
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.colorSuccess));
                break;
            case TaskRecord.STATUS_REJECTED:
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.colorError));
                break;
            default:
                holder.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.colorWarning));
                break;
        }
        
        // 格式化并设置完成时间
        try {
            Date date = inputFormat.parse(record.getCompletionTime());
            if (date != null) {
                holder.tvCompletionTime.setText("完成时间: " + outputFormat.format(date));
            } else {
                holder.tvCompletionTime.setText("完成时间: " + record.getCompletionTime());
            }
        } catch (ParseException e) {
            holder.tvCompletionTime.setText("完成时间: " + record.getCompletionTime());
        }
        
        // 格式化并设置审核时间
        try {
            Date date = inputFormat.parse(record.getApprovalTime());
            if (date != null) {
                holder.tvApprovalTime.setText("审核时间: " + outputFormat.format(date));
            } else {
                holder.tvApprovalTime.setText("审核时间: " + record.getApprovalTime());
            }
        } catch (ParseException e) {
            holder.tvApprovalTime.setText("审核时间: " + record.getApprovalTime());
        }
    }

    @Override
    public int getItemCount() {
        return taskRecords.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTaskName;
        TextView tvTaskDescription;
        TextView tvReward;
        TextView tvStatus;
        TextView tvCompletionTime;
        TextView tvApprovalTime;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTaskName = itemView.findViewById(R.id.tv_task_name);
            tvTaskDescription = itemView.findViewById(R.id.tv_task_description);
            tvReward = itemView.findViewById(R.id.tv_reward);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvCompletionTime = itemView.findViewById(R.id.tv_completion_time);
            tvApprovalTime = itemView.findViewById(R.id.tv_approval_time);
        }
    }
} 